package com.example.xyr.myapplication.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.CheckBox
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.api.BasicCallback
import com.example.xyr.myapplication.Constant
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.myinterface.ActivityCallback
import com.example.xyr.myapplication.utils.MyIntent
import com.example.xyr.myapplication.utils.StringUtil
import com.example.xyr.myapplication.utils.TextUtil
import kotlinx.android.synthetic.main.activity_revise_user_name.*
import kotlinx.android.synthetic.main.titlebar_layout.*

/**
 * Created by ylcf on 2018/8/28.
 */
class ModifyNameActivity : BaseActivity(), View.OnClickListener {

    companion object {
        private lateinit var activity: Activity
        private var frrequestCode: Int = -1
        private var mType: Type = Type.ModifyName

        fun initActivity(fromActivity: Activity, requestCode: Int, type: Type) {
            activity = fromActivity
            frrequestCode = requestCode
            mType = type
            fromActivity.startActivity(Intent(fromActivity, ModifyNameActivity::class.java))
        }
    }

    override fun getLayout(): Int = R.layout.activity_revise_user_name

    override fun init() {
        //当前为修改昵称
        if (mType == Type.ModifyName) {
            lly_modify_name.visibility = View.VISIBLE
            rl_modify_gender.visibility = View.VISIBLE
            lly_modify_password.visibility = View.GONE
            titleBar.setTitle("修改爱称")
            titleBar.setRightString("完成")
        } else {
            //当前为修改密码
            titleBar.setTitle("修改密码")
            titleBar.setRightString("完成")
            lly_modify_password.visibility = View.VISIBLE
            rl_modify_gender.visibility = View.GONE
            lly_modify_name.visibility = View.GONE
            var oldStr = TextUtil.instance.setTextColor("旧密码", resources.getColor(R.color.my_text_blue), 1)
            var newStr = TextUtil.instance.setTextColor("新密码", resources.getColor(R.color.my_text_blue), 1)
            var confirmStr = TextUtil.instance.setTextColor("确认密码", resources.getColor(R.color.my_text_gray), 2)
            tv_old.text = oldStr
            tv_new.text = newStr
            tv_confirm.text = confirmStr
        }
    }

    override fun initListener() {
        titleBar.ivBack.setOnClickListener(this)
        titleBar.tvRight.setOnClickListener(this)
        setChickBox(cb_modify_boy, cb_modify_girl)
        setChickBox(cb_modify_girl, cb_modify_boy)
    }

    override fun initDate(intent: MyIntent?) {
        val nickname = JMessageClient.getMyInfo().nickname
        if (StringUtil.isNotEmpty(nickname)) {
            revise_clear.hint = nickname
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
            R.id.tv_right -> onComplte()
        }
    }

    fun onComplte() {
        if (mType == Type.ModifyName) {
            val nickName = revise_clear.getText().toString()
            if (StringUtil.isNotEmpty(nickName) && frrequestCode != -1) {
                val intent = intent.putExtra(Constant.NICK_NAME, nickName)
                intent.putExtra(Constant.GENDER, getGener())
                if (activity is ActivityCallback) {
                    (activity as ActivityCallback).onActivityCallBack(Constant.REVISE_USER_NAME, frrequestCode, intent)
                } else {
                    setResult(Activity.RESULT_OK, intent)
                }
                finish()
            }
        } else {
            var oldPass = old_pass_clear.text.toString()
            var newPass = new_pass_clear.text.toString()
            var confirmPass = confirm_pass_clear.text.toString()
            var isContinue = TextUtil.instance.judgePass(this, oldPass, newPass, confirmPass)
            if (isContinue) {
                return
            }
            //修改密码
            JMessageClient.updateUserPassword(oldPass, newPass, object : BasicCallback() {
                override fun gotResult(p0: Int, p1: String?) {
                    if (p0 == 0) {
                        toastOk("修改成功")
                        finish()
                    }
                }

            })
        }
    }

    /**
     * 设置CheckBox选中事件
     */
    fun setChickBox(chickBox1: CheckBox, chickBox2: CheckBox) {
        chickBox1.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                chickBox2.isChecked = false
            }
        }
    }

    fun getGener(): String {
        return if (cb_modify_boy.isChecked) {
            "boy"
        } else {
            "girl"
        }
    }

    enum class Type {
        ModifyName, ModifyPassWord
    }

}